/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mysqlviewer.model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import mysqlviewer.gui.DialogPesan;

/**
 *
 * @author agoesh
 */
public class ModelJList extends AbstractListModel{
    private String[] daftarTabel;
    
    public ModelJList(Connection c, String namaDB){
        super();
        buatDaftarTabel(c, namaDB);
    }
    
    public int getSize() {
        return daftarTabel.length;
    }

    public Object getElementAt(int index) {
        return daftarTabel[index];
    }

    private void buatDaftarTabel(Connection c, String namaDB) {
        if(namaDB!=null){
            try{
                Statement state=c.createStatement();
                state.executeQuery("USE "+namaDB);
                ResultSet hasil=state.executeQuery("SHOW TABLES");

                ArrayList dataSem=new ArrayList();
                while(hasil.next()){
                    dataSem.add(hasil.getString(1));
                }
                daftarTabel=new String[dataSem.size()];
                dataSem.toArray(daftarTabel);
            }catch(Exception e){
                new DialogPesan(null, true, e.getMessage());
            }
        }
    }

}
